package com.wangyadong.hobby.thread.io;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2019/7/116:32
 **/
public class TypeServer extends TCPThrottledServer

{


    @Override
    public void run(Socket data) {
        try {
            DataOutputStream dos = new DataOutputStream(data.getOutputStream());
            dos.writeByte(TypeServerConstants.WELCOME);

            DataInputStream dis = new DataInputStream(data.getInputStream());
            byte b = dis.readByte();
            if (b != TypeServerConstants.GET_STRING_REQUEST) {
                System.out.println("Client sent unknow request" + b);
                return;
            }

            dos.writeByte(TypeServerConstants.GET_STRING_REQUEST);
            dos.writeUTF("this is a test string");
            dos.flush();
        } catch (IOException e) {
            System.out.println("Client terminating:" + e);
            return;
        } finally {
            try {
                data.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        super.run(data);
    }

    public static void main(String[] args) throws IOException {
        TypeServer server = new TypeServer();
        server.startServer(Integer.valueOf(args[0]), Integer.valueOf(args[1]));
        System.out.println("Server ready and waiting");
    }

    public final String flag = "dddd";

    public void testSyn() {
        synchronized (flag) {

        }
    }
}
